import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '@/views/home.vue'
import car from '@/views/car.vue'
import list from '@/views/list.vue'
import my from '@/views/my.vue'
import search from '@/views/search.vue'
import searchIndex from  '@/views/search/index.vue'
import searchList from  '@/views/search/searchList.vue'
import detail from '@/views/detail.vue'
import login from '@/views/login/index.vue'
import loginIndex from '@/views/login/login.vue'
import resgister from "@/views/login/register.vue"
import password from "@/views/login/passwordLogin.vue"
import recovery from "@/views/passwordrecovery/recovery.vue"
import recoverybtn from "@/views/passwordrecovery/recoverybtn.vue"
import path from "@/views/path/index.vue"
import pathMessage from "@/views/path/pathMessage.vue"
import newAdress from "@/views/path/newAdress.vue"
import order from "@/views/order.vue"
import payment from "@/views/payment.vue"

// 解决报错
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: home
  },
  // 重定向
  {
    path: '/',
    redirect: '/home',

  },
  {
    path: '/login',
    component:login,
    children: [
      {
        path: '/',
        name: 'loginIndex',
        component: password
      },
      {
        path: '/login/resgister',
        name: 'resgister',
        component: resgister
      },
      {
        path: '/login/msglogin',
        name: 'password',
        component: loginIndex
      },
      

    ]
  },
  // 找回密码
  {
    path: '/passwordrecovery',
    component:login,
    children: [
      {
        path: '/',
        name: 'recovery',
        component: recovery
      },
      {
        path: '/passwordrecovery/recoverybtn',
        name: 'recoverybtn',
        component: recoverybtn
      },
     
      

    ]
  },
  {
    path: '/car',
    name: 'car',
    component: car
  },
  {
    path: '/list',
    name: 'list',
    component: list
  },
  {
    path: '/my',
    name: 'my',
    component: my
  },
  {
    path: '/detail',
    name: 'detail',
    component: detail,
  },
  {
    path: '/search',
    component: search,
    children: [
      {
        path: '/',
        name: 'index',
        component: searchIndex
      },
      {
        path: '/search/list',
        name: 'searchList',
        component: searchList
      },

    ]
  },
  // 地址管理
  {
    path: '/path',
   
    component: path,
    
    children:[
      {
        path: '/',
        name: 'pathMessage',
        component: pathMessage,
        // meta: {keepAlive: true}, //true缓存 false不缓存
      },
      {
        path: '/path/newAdress',
        name: 'newAdress',
        component: newAdress
      },
    ]
  },
   // 提交订单页
   {
    path: '/order',
    name:'order',
    component: order,
    // meta: {keepAlive: true}, //true缓存 false不缓存
  },
  {
    path: '/payment',
    name:'payment',
    component: payment,
    // meta: {keepAlive: true}, //true缓存 false不缓存
  },

]

const router = new VueRouter({
  routes
})

// 全局拦截守卫
router.beforeEach((to,from,next)=>{
  let nextRouter=['pathMessage','car','payment','order']
  // 是否登陆中
  
  let userInfo=JSON.parse(localStorage.getItem('userInfo')) 

  // 当前进入的页面
  if(nextRouter.indexOf(to.name)>=0){
      if(!userInfo){
        router.push('login')
      }
  }
  next()

})

export default router
